* autofills forms found in the given document with there respective templates if they exist
*/
Lazarus.autofillDoc = function(doc){
if (Lazarus.isValidDoc(doc) && doc.forms && doc.forms.length){
var rsAutoFillTemplates = Lazarus.db.rs("SELECT id, formid FROM forms WHERE autofill = 1 AND savetype = "+ Lazarus.FORM_TYPE_TEMPLATE +" ORDER BY created DESC");
if (rsAutoFillTemplates.length > 0){
var templates = {};
//convert the template id's to a hash table
//TODO: there should only ever be one autofill template for a given form
var record = Lazarus.db.getRow("SELECT id, text_hash FROM textdata WHERE domain_hash = ?1 AND savetype = ?2 LIMIT 1", info.domainHash, Lazarus.FORM_TYPE_AUTOSAVE);
//if form hasn't changed
if (record && record.text_hash == textHash){
//do nothing...
}
//if it has changed, or doesn't exist yet, save the changes
Lazarus.db.exe("DELETE FROM textdata WHERE id = ?1", record.id);
//and get rid of any fulltext index as well
Lazarus.db.exe("DELETE FROM textdata_fulltext WHERE docid = ?1", record.id);
}
var encText = Lazarus.encrypt(info.text);
var encSummary = Lazarus.encrypt(info.summary);
var id = Lazarus.db.insert("INSERT INTO textdata (text_encrypted, summary_encrypted, created, domain_hash, url_encrypted, text_hash, text_length, savetype) \
Lazarus.db.exe("INSERT INTO textdata_fulltext (docid, hashed_text) VALUES (?1, ?2)", id, hashedText);
}
}
}
else {
//if the info object already exists, with exactly the same text, then just update the timestamp
var id = Lazarus.db.getInt("SELECT id FROM textdata WHERE domain_hash = ?1 AND text_hash = ?2 LIMIT 1", info.domainHash, textHash);
if (id){
Lazarus.debug("Updating textdata - perm", info);
Lazarus.db.exe("UPDATE textdata SET created = ?1, savetype = ?2 WHERE id = ?3", info.created, Lazarus.FORM_TYPE_NORMAL, id);
}
//otherwise, insert a new info object
else {
Lazarus.debug("Saving textdata - perm", info);
var encText = Lazarus.encrypt(info.text);
var encSummary = Lazarus.encrypt(info.summary);
var id = Lazarus.db.insert("INSERT INTO textdata (text_encrypted, summary_encrypted, created, domain_hash, url_encrypted, text_hash, text_length, savetype) \
var menu = Lazarus.$("lazarus-restoretext-submenu-menupopup");
//remove all the current submenu items
while(menu.lastChild){
menu.removeChild(menu.lastChild);
}
var domainHash = Lazarus.md5(Lazarus.getDomainFromElement(editor));
//and build the new ones
var items = Lazarus.db.rs("SELECT id, text_hash, summary_encrypted, created FROM textdata WHERE domain_hash = ?1 ORDER BY created DESC LIMIT ?2", domainHash, Lazarus.getPref('extensions.lazarus.maxTextItemsInSubmenu', 20));
var text = Lazarus.extractText(editor);
var textHash = Lazarus.md5(text);
for (var i=0; i<items.length; i++){
var item = items[i];
var menuitem = document.createElement("menuitem");
if (Lazarus.getExtPref("showFormTextInSubMenu") && Lazarus.canDecrypt()){
var summary = Lazarus.decrypt(item["summary_encrypted"]);
var numChars = Lazarus.formatNumber(charsRestored);
var numForms = Lazarus.formatNumber(Lazarus.db.getInt("SELECT COUNT(*) FROM forms"));
var numText = Lazarus.formatNumber(Lazarus.db.getInt("SELECT COUNT(*) FROM textdata"));
var msgs = [
'Lazarus has just saved you from having to retype '+ numChars +' characters. If you feel this has helped you, then please consider donating to this project so we can make Lazarus even better.',
'Lazarus restored '+ numChars +' characters.\nDatabase contains '+ numForms +' Forms and '+ numText +' Text-blocks.',
'Lazarus restored '+ numChars +' characters.\nDatabase contains '+ numForms +' Saved Forms and '+ numText +' Textareas.',
'Lazarus resurrected '+ numChars +' characters.\nLazarus is securely storing '+ numForms +' Forms and '+ numText +' Text-blocks'
//we need to get ALL the templates for this form first
var forms = [];
forms = forms.concat(Lazarus.db.rs("SELECT "+ fields +" FROM forms WHERE formid = ?1 AND created >= ?2 AND savetype = "+ Lazarus.FORM_TYPE_TEMPLATE +" ORDER BY savetype DESC, created DESC", formId, cutoffTime));
//and then the autosaves
var maxAutoSaves = Lazarus.getExtPref("maxAutosavesPerForm", 3);
forms = forms.concat(Lazarus.db.rs("SELECT "+ fields +" FROM forms WHERE formid = ?1 AND created >= ?2 AND savetype IN ("+ Lazarus.FORM_TYPE_AUTOSAVE +","+ Lazarus.FORM_TYPE_STALE_AUTOSAVE +") ORDER BY created DESC LIMIT ?3", formId, cutoffTime, maxAutoSaves));
//and then add normal saved forms
var maxForms = Lazarus.getExtPref("maxSavesPerForm", 10);
forms = forms.concat(Lazarus.db.rs("SELECT "+ fields +" FROM forms WHERE formid = ?1 AND created >= ?2 AND savetype = "+ Lazarus.FORM_TYPE_NORMAL +" ORDER BY created DESC LIMIT ?3", formId, cutoffTime, maxForms));
return forms;
}
/**
* return a fieldInfo object filled with information about the given field
*/
Lazarus.fieldInfo = function(ele){
var getPassword = Lazarus.getExtPref("savePasswordFields");
var getHidden = Lazarus.getExtPref("saveHiddenFields");
var info = {};
info.name = ele.getAttribute("name");
info.type = Lazarus.getElementType(ele);
info.value = Lazarus.getElementValue(ele);
if (info.type == "password" && !getPassword){
info.value = null;
}
if (info.type == "hidden" && !getHidden){
info.value = null;
}
switch (info.type){
case "text":
case "textarea":
case "file":
case "password":
case "iframe":
if (info.value && Lazarus.trim(info.value)){
info.text = Lazarus.trim(info.value);
}
break;
default:
}
return info;
}
/**
* clean HTML tags and entities from an html string
//No. what about forms with lots of radio buttons (eg multichoice surveys)
var infoJSON = Lazarus.JSON.encode(info)
var encryptedInfo = Lazarus.encrypt(infoJSON);
var encryptedText = Lazarus.encrypt(Lazarus.getMenuItemText(info.formtext));
var encryptedFormURL = Lazarus.encrypt(Lazarus.getFormURL(form));
var infoHash = Lazarus.generateHash(info.fields);
//each form type needs to be saved differently
switch(formType){
case Lazarus.FORM_TYPE_AUTOSAVE:
case Lazarus.FORM_TYPE_STALE_AUTOSAVE:
var savedForm = Lazarus.db.getRow("SELECT id, savetype FROM forms WHERE formid = ?1 AND forminfohash = ?2 AND savetype IN ("+ Lazarus.FORM_TYPE_AUTOSAVE +","+ Lazarus.FORM_TYPE_STALE_AUTOSAVE +") LIMIT 1", info.formid, infoHash);
//add or update the current autosave
if (!savedForm){
var lastAutoSaveId = Lazarus.db.getInt("SELECT id FROM forms WHERE formid = ?1 AND savetype = "+ Lazarus.FORM_TYPE_AUTOSAVE +" ORDER BY created DESC LIMIT 1", info.formid);
//and save this forminfo as the last autosave point
Lazarus.lastAutoSaveForm = info;
}
else {
Lazarus.debug("No need to autosave form, form already exists: "+ info.formid);
}
break;
case Lazarus.FORM_TYPE_NORMAL:
//delete all autosaves
Lazarus.removeForms(Lazarus.db.getColumn("SELECT id FROM forms WHERE formid = ?1 AND savetype IN ("+ Lazarus.FORM_TYPE_AUTOSAVE +","+ Lazarus.FORM_TYPE_STALE_AUTOSAVE +")", info.formid));
//if this form already exists, delete it
Lazarus.removeForms(Lazarus.db.getColumn("SELECT id FROM forms WHERE formid = ?1 AND forminfohash = ?2 AND savetype = "+ Lazarus.FORM_TYPE_NORMAL, info.formid, infoHash));
//and save this form
Lazarus.db.exe("INSERT INTO forms (formid, created, forminfo, formtext, forminfohash, formname, formurl, text_length, savetype, autofill) \
* convert autosaved forms to semi-permanent save points
*/
Lazarus.saveAutoSavedForms = function(){
//convert autosaved forms to semi-permanent points
var rs = Lazarus.db.rs("SELECT id, formid FROM forms WHERE savetype = "+ Lazarus.FORM_TYPE_AUTOSAVE +" ORDER BY formid, created DESC");
var lastFormId = '';
var lastFormCnt = 0;
var MAX_AUTOSAVES_TO_KEEP = 2;
for (var i=0; i<rs.length; i++){
var savedForm = rs[i];
if (lastFormId != savedForm["formid"]){
lastFormId = savedForm["formid"];
lastFormCnt = 1;
}
else {
lastFormCnt++;
}
if (lastFormCnt <= MAX_AUTOSAVES_TO_KEEP){
Lazarus.db.exe('UPDATE forms SET savetype = '+ Lazarus.FORM_TYPE_STALE_AUTOSAVE +' WHERE id = ?1', savedForm["id"]);
}
else {
//excess saved form, remove it
Lazarus.removeForms(savedForm["id"]);
}
}
}
/**
* removes old/excess forms from the database
*/
Lazarus.removeOldForms = function(){
var rs = Lazarus.db.rs("SELECT count(*) as cnt, formid FROM forms WHERE savetype = "+ Lazarus.FORM_TYPE_NORMAL +" GROUP BY formid");
var maxForms = Lazarus.getExtPref("maxSavesPerForm", 10);
for (var i=0; i<rs.length; i++){
var save = rs[i];
if (save["cnt"] > maxForms){
Lazarus.debug("Removing excess forms "+ save["formid"]);
var ids = Lazarus.db.getColumn("SELECT id FROM forms WHERE formid = ?1 AND savetype = "+ Lazarus.FORM_TYPE_NORMAL +" ORDER BY created DESC LIMIT -1 OFFSET ?2", save["formid"], maxForms);
if (ids.length){
//and remove
Lazarus.debug("cleaning up forms "+ ids.join(","));
Lazarus.removeForms(ids);
}
}
}
}
/**
* empties all the tables in the database
* except those specified.
*/
Lazarus.emptyDB = function(ignoreFormTypes){
//always remove text data
Lazarus.db.exe('DELETE FROM textdata');
Lazarus.db.exe('DELETE FROM textdata_fulltext');
if (typeof ignoreFormTypes == "undefined"){
Lazarus.db.exe('DELETE FROM forms');
Lazarus.db.exe('DELETE FROM forms_fulltext');
}
else {
var ids = Lazarus.db.getColumn('SELECT id FROM forms WHERE savetype NOT IN ('+ ignoreFormTypes +')');
var nsICryptoHash = Components.classes['@mozilla.org/security/hash;1'].createInstance(Components.interfaces.nsICryptoHash);
var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
// result is an out parameter,
// result.value will contain the array length
var result = {};
// data is an array of bytes
var data = converter.convertToByteArray(str, result);
var newClassname = ele.getAttribute('class').toLowerCase().replace(classname.toLowerCase(), "").replace(/\s+/g, ' ').replace(/^\s+|\s+$/g, '');
ele.setAttribute('class', newClassname);
}
}
/**
*
*/
Lazarus.test = function(evt){
Lazarus.cleanDB();
return;
alert(1);
Lazarus.backgroundTask(function(){
var x = 1234
for(var i=0; i<20000000; i++){
x = x/100;
}
//try throwing an error in the middle of the function
x = a.b.c;
return x;
}, function(result){
alert(result);
})
return;
alert("Private = "+ Lazarus.isPrivateBrowsing());
return;
//~ Unit.assert("Lazarus.canEncrypt()", true, "should be able to encrypt");
//~ alert(Unit.getLog());
//~ return;
var len = 10;
var p = new Profiler("decrypt");
var enc = [];
var dec = [];
for (var i=0; i<len; i++){
enc[i] = Lazarus.encrypt("abc"+ Math.random());
}
p.mark("encrypted "+ len +" strings");
for (var i=0; i<len; i++){
dec[i] = Lazarus.decrypt(enc[i]);
}
debug(p.stop("decrypted"));
return;
debug(Lazarus.db.rs("SELECT hashed_text FROM textdata_fulltext LIMIT 1"));
return;
Lazarus.openTextManager();
return;
var str = prompt("Enter search string");
if (str){
debug(str, Lazarus.hashQuery(str));
var rs = Lazarus.db.rs("SELECT id, text_encrypted FROM textdata JOIN textdata_fulltext ON textdata.id = textdata_fulltext.docid WHERE textdata_fulltext.hashed_text MATCH ?1", Lazarus.hashQuery(str));
debug(rs);
var text = '';
for (var i=0; i<rs.length; i++){
text += rs[i].id +":"+ Lazarus.decrypt(rs[i].text_encrypted) +"\n";